package com.jxs.android.weixi;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jxs.android.util.DetainUtils;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.TopBar;
import com.telecom.weixi.R;

public class MyWork_Customer extends LoadingMoreAbstractList {
	private int REQ = 0x124;
	private CstInfoAdapter adapter;

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		getWebServiceData(WeixiApplication.getUserInfo().getTicket(), Which.MY_WORK_CUSTOMER, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, PAGE_SIZE, currentPage);
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new CstInfoAdapter(context);
		return adapter;
	}

	@Override
	public Object paserData(SoapObject retMsgObject, Object... params) {
		List<WrkCstInfo> tems = new ArrayList<MyWork_Customer.WrkCstInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				WrkCstInfo cstInfo = new WrkCstInfo();
				cstInfo.cust_id = taskBeanObject.getPropertySafelyAsString("cust_id");
				cstInfo.cust_name = taskBeanObject.getPropertySafelyAsString("cust_name");
				cstInfo.latn_id = taskBeanObject.getPropertySafelyAsString("latn_id");
				cstInfo.custarpu_val = taskBeanObject.getPropertySafelyAsString("custarpu_val");
				cstInfo.acct_info = taskBeanObject.getPropertySafelyAsString("acct_info");
				cstInfo.detain_info = taskBeanObject.getPropertySafelyAsString("detain_info");
				cstInfo.product_info = taskBeanObject.getPropertySafelyAsString("product_info");
				tems.add(cstInfo);
			}
		}
		return tems;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateList(Message msg) {
		List<WrkCstInfo> tems = (List<WrkCstInfo>) msg.obj;
		empytText.setVisibility(View.GONE);

		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				refreshListView.removeFooterView(loadMoreView);
			}
		}
		adapter.setCstInfos(tems);
		adapter.notifyDataSetChanged();
		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			currentPage++;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQ && resultCode == RESULT_OK) {
			if (DetainUtils.isRemove()) {
				adapter.removeCstInfo(data.getExtras().getInt("position"));
				adapter.notifyDataSetChanged();
			}
		}
	}

	class CstInfoAdapter extends BaseAdapter {

		List<WrkCstInfo> wrkCstInfos = new ArrayList<WrkCstInfo>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public CstInfoAdapter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setCstInfos(List<WrkCstInfo> wrkCstInfos) {
			this.wrkCstInfos.addAll(wrkCstInfos);
		}

		public void removeCstInfo(int position) {
			this.wrkCstInfos.remove(position);
		}

		@Override
		public int getCount() {
			return wrkCstInfos.size();
		}

		@Override
		public WrkCstInfo getItem(int position) {
			return wrkCstInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final WrkCstInfo wrkCstInfo = wrkCstInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_list_item_work_customer, null);
				holder = new ViewHolder();
				holder.position = (TextView) convertView.findViewById(R.id.position);
				holder.cust_name = (TextView) convertView.findViewById(R.id.cust_name);
				holder.latn_id = (TextView) convertView.findViewById(R.id.latn_id);
				holder.custarpu_val = (TextView) convertView.findViewById(R.id.custarpu_val);
				holder.acct_info = (TextView) convertView.findViewById(R.id.acct_info);
				holder.product_info = (TextView) convertView.findViewById(R.id.product_info);
				holder.detain_info = (TextView) convertView.findViewById(R.id.detain_info);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.position.setText("No." + (position + 1));
			holder.cust_name.setText(wrkCstInfo.cust_name);
			holder.latn_id.setText(wrkCstInfo.latn_id);
			holder.custarpu_val.setText(wrkCstInfo.custarpu_val);
			holder.acct_info.setText(wrkCstInfo.acct_info);
			holder.product_info.setText(wrkCstInfo.product_info);
			holder.detain_info.setText(wrkCstInfo.detain_info);

			convertView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Intent intent = new Intent(MyWork_Customer.this, MyWork_Customer_Detain.class);
					Bundle bundle = new Bundle();
					bundle.putString("title", "我的政企客户维系执行");
					bundle.putString("cust_id", wrkCstInfo.cust_id);

					bundle.putString("name_label", getString(R.string.cust_name));
					bundle.putString("name_value", wrkCstInfo.cust_name);
					bundle.putString("detain_label", getString(R.string.detain_info));
					bundle.putString("detain_value", wrkCstInfo.detain_info);

					bundle.putInt("position", position);
					intent.putExtras(bundle);
					startActivityForResult(intent, REQ);
				}
			});

			return convertView;
		}

		class ViewHolder {
			TextView position;// 客户名称
			TextView cust_name;// 客户名称
			TextView latn_id;// 本地网编码
			TextView custarpu_val;// 客户ARPU
			TextView acct_info;// 账户信息
			TextView product_info;// 产品信息
			TextView detain_info;// 维系信息
		}

	}

	class WrkCstInfo {

		String cust_id;// 客户编码
		String cust_name;// 客户名称
		String latn_id;// 本地网编码
		String custarpu_val;// 客户ARPU
		String acct_info;// 账户信息
		String product_info;// 产品信息
		String detain_info;// 维系信息
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		topBar.hindBtn();
		topBarReturn(topBar);
		topBar.setTitle(title);
	}

	@Override
	public String getMethod() {
		return "getDetainCustPage";
	}

}
